import {ref,reactive} from 'vue'
import {zdygz} from './list'
import {addList} from '@/api/cateList/list'
import { message } from 'ant-design-vue';
import {updateList} from '@/api/cateList/list'
let {data,methodsGet} = zdygz()
let showxs = ref<boolean>(false)
export const addcate = ref<boolean>(false);
export const showModal = () => {
  addcate.value = true;
};
export let lable=ref<string>('添加分类')
interface FormState {
name: string,
_id:string
}
export const formState = reactive<FormState>({
    name: '',
    _id:''
  });
export function addlist(){
  showModal()
  console.log(formState)
  title.value='添加分类'
  lable.value='添加分类'
  formState.name=''
  showxs.value=false
}
export let title=ref<string>('添加分类')




export function dj(e:any){
showModal()
title.value='更改分类'
lable.value='更改分类'
console.log(e)
let {name,_id}=e
formState.name=name
formState._id=_id
showxs.value=true
console.log(showxs.value)
}



const handleOk = (cd:Function) => {
       
  console.log(formState)

  let {name,_id}=formState

 
  if(showxs.value==true){

updateList({
  cate_id:_id,
  cate_name:name
}).then((res:any)=>{
  console.log(res)
  if(res.code==0){
  addcate.value = false;
     message.success(res.msg)
    cd()
  }else{
    message.warning(res.msg)
  }
})
  }else{

  
  if( formState.name.length==0){
      message.warning('请输入添加分类名')
      return
  }
  addList({name}).then((res:any)=>{
    console.log(res)
  
    if(res.code==0){
        message.success(res.msg)
        addcate.value = false;
        cd()
        formState.name=''
    }else{
      message.warning(res.msg)
       formState.name=''
    }
  })
}
  };
// export function succes(){
   
    export const hd=()=>{
          handleOk(methodsGet)
        }
       
// }